package com.atguigu.crowd.service.impl;

import java.util.List;

import com.atguigu.crowd.entity.ShengExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atguigu.crowd.entity.Sheng;
import com.atguigu.crowd.mapper.ShengMapper;
import com.atguigu.crowd.service.api.ShengService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class ShengServiceImpl implements ShengService {

	@Autowired
	ShengMapper shengMapper;
	
	//分页显示页面的service方法(同步和异步能不能共用呢？答案是：可以一样（前端不做统一JSON格式处理），也可以不一样-前端做了统一JSON格式处理的话，返回值要封装)
	public PageInfo<Sheng> getPage(Integer pageNum,Integer pageSize,String keyword ) {
		
		//1、调用pageHelper的静态方法开启分页功能
		//这里充分体现了pageHelper的"非侵入式"设计，原本要做的查询不需要任何的修改
		PageHelper.startPage(pageNum, pageSize);
		
		List<Sheng> list=shengMapper.seleShengByKeyword(keyword);

		PageInfo<Sheng> pageInfo=new PageInfo<>(list);
		
		return pageInfo;
	}

	@Override
	public Sheng getShengByPrimaryKey(Integer id) {

		return shengMapper.selectByPrimaryKey(id);
	}

	@Override
	public int shengAddOne(Sheng sheng) {

		return shengMapper.insertSelective(sheng);
	}
	/**
	 * 添加省份前判断省份名称是否重复
	 */
	@Override
	public int existShengNameCount(String shengName) {
		
		return shengMapper.seleByshengName(shengName);
	}

	/**
	 * 执行修改
	 */
	@Override
	public void updateByExampleSelective(Sheng sheng) {
		
		shengMapper.updateByPrimaryKeySelective(sheng);
	}
	/**
	 * 根据id删除数据
	 */
	@Override
	public void deleteById(Integer id) {
		
		shengMapper.deleteByPrimaryKey(id);
	}
	/**
	 * 执行更新
	 */
	@Override
	public void updateSheng(Sheng sheng) {
	
		shengMapper.updateByPrimaryKey(sheng);
	}

	/**
	 * 批量删除和单条删除都可用
	 * @param shengids
	 */
	@Override
	public void removeShengs(List<Integer> shengids) {

		//方式1：
		/*for(Integer id :shengids){
			shengMapper.deleteByPrimaryKey(id);
		}*/
		//方式2
		ShengExample example=new ShengExample();
		ShengExample.Criteria criteria=example.createCriteria();
		criteria.andShengidIn(shengids);
		shengMapper.deleteByExample(example);

	}
}















